package thread;

import 网络编程.MyServerSocket;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class ClientReceiveThread extends Thread {
    private Socket socket;

    public ClientReceiveThread(Socket socket) {
        this.socket = socket;

    }

    @Override
    public void run() {

        try {
            InputStream inputStream = socket.getInputStream();
            DataInputStream dataInputStream = new DataInputStream(inputStream);
            while (true) {
                try {
                    String msg = dataInputStream.readUTF();
                    System.out.println( msg );
                } catch (Exception e) {
                System.out.println("自己下线了");
                MyServerSocket.onLineSockets.remove(socket);
                dataInputStream.close();
                socket.close();
                break;
            }
                
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
